#include "recboxitem.h"
#include "ui_recboxitem.h"

RecBoxItem::RecBoxItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //安装过滤器到指定目标对象
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->musicImageBox)
    {
        if(event->type() == QEvent::Enter)
        {
            // 添加图片上移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(150);
            animation->setStartValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            //动画完成 会发射 finish信号
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
        }
        else if(event->type() == QEvent::Leave)
        {
            // 添加图片下移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(150);
            animation->setStartValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
        }
    }
    //其他类型事件交给父类处理
    return QObject::eventFilter(watched, event);
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(const QString &imagePath)
{
    QString imageStyle = "background-image:url(" + imagePath + ");";
    ui->musicImageBox->setStyleSheet(imageStyle);
}
